package wodegua.d24_file_io;

import java.io.*;

/**
 * 原始流、缓冲流的性能分析[重点]
 *
 * 测试用例:
 * 分别使用原始的字节流，以及字节缓冲流复制一个很大视频。
 *
 * 测试步骤:
 * 1、使用低级的字节流按照一个一个字节的形式复制文件。
 * 2、使用低级的字节流按照字节数组的形式复制文件。
 * 3、使用高级的缓冲字节流按照一个一个字节的形式复制文件，
 * 4、使用高级的缓冲字节流按照字节数组的形式复制文件。
 */
public class d18_IoTimeTest {
    private static String inSrc = "E:\\素材\\5月出差视频素材5.25\\IMG_2125.MOV";
    private static String outSrc = "E:\\data\\";

    public static void main(String[] args) {
//        FileStream(); //98.588秒
        FileStreamArray(); //0.134秒
        BufferedStream(); //0.531秒
        BufferedStreamArray(); //0.048秒
    }

    private static void FileStream(){
        long startTime = System.currentTimeMillis();
        try (
                InputStream is = new FileInputStream(inSrc);
                OutputStream os = new FileOutputStream(outSrc + "1.mov");
        ) {
            int c;
            while ((c = is.read()) != -1) {
                os.write(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        time(startTime, endTime, "低级字节流一个一个字节复制");
    }

    private static void FileStreamArray(){
        long startTime = System.currentTimeMillis();
        try (
                InputStream is = new FileInputStream(inSrc);
                OutputStream os = new FileOutputStream(outSrc + "2.mov");
        ) {
            byte[] buffer = new byte[1024 * 8];
            int len;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        time(startTime, endTime, "低级字节流使用字节数组复制");
    }

    private static void BufferedStream(){
        long startTime = System.currentTimeMillis();
        try (
                InputStream bis = new BufferedInputStream(new FileInputStream(inSrc));
                OutputStream bos = new BufferedOutputStream(new FileOutputStream(outSrc + "3.mov"));
        ) {
            int c;
            while ((c = bis.read()) != -1) {
                bos.write(c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        time(startTime, endTime, "缓冲流一个一个字节复制");
    }

    private static void BufferedStreamArray(){
        long startTime = System.currentTimeMillis();
        try (
                InputStream bis = new BufferedInputStream(new FileInputStream(inSrc));
                OutputStream bos = new BufferedOutputStream(new FileOutputStream(outSrc + "4.mov"));
        ) {
            byte[] buffer = new byte[1024 * 8];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        time(startTime, endTime, "缓冲流使用字节数组复制");
    }

    private static void time(long startTime, long endTime, String title){
        System.out.println(title + "耗时：" + (endTime - startTime) / 1000.0 + "秒");
    }
}
